<?php

namespace IDock\Insurance\AICS\Response;

use IDock\Http\Response;
use IDock\Insurance\ACIC\Response\AllACICResponse;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\NonCar\NonCarUnderwritingPrinter;
use IDock\Printer\PremiumUnderwritingPrinter;
use IDock\Visitor\NonCar\NonCarProductVisitor;

class UnderwritingResponse extends AllACICResponse
{
    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = str_replace('&lt;','<',$body);
        $body = str_replace('&gt;','>',$body);
        $body = str_replace("'",'"',$body);
        $start = strpos($body,'<response>');
        $end = strpos($body,'</response>');
        $body = substr($body,$start + 10,intval($end - $start -10));
        if ($body) {
            libxml_disable_entity_loader(false);

            $body = simplexml_load_string($body);
            $body = $this->objectToArray($body);
            $errorCode = $body['responseHead']['returnCode'] ?? '';
            $message = $body['responseHead']['returnMessage'] ?? '未知错误';
            $this->setBody($body);
            if ($errorCode !== '0000') {
                $this->setError($message);
                return $this;
            }
        }
        return $this;
    }

    public function printerInit(): Response
    {

        $data = $this->getBody();
        $premiumUnderwritingPrinter = new PremiumUnderwritingPrinter();

        $premiumUnderwritingPrinter->inquiryNo = $data['orderCode'] ?? '';//订单号

        $premiumUnderwritingPrinter->biInquiryNo = $data['policyAppNoSY'] ?? '';

        $premiumUnderwritingPrinter->ciInquiryNo = $data['policyAppNoJQ'] ?? '';

        $biStats = $data['statusSY'] ?? '0';
        $biTip = $data['ciResponseMessagedate']['CMessage'] ?? '';

        $ciStats = $data['statusJQ'] ?? '0';
        $ciTip = $data['sailResponseMessagedate']['CMessage'] ?? '';
        if(!empty($ciStats) && !empty($biStats)){
            if (in_array($ciStats,[1,9]) and in_array($biStats,[1,9])) {
                $premiumUnderwritingPrinter->statusCode = 'U1';//已核保
            }
        }else{
            $tmpStatus = $biStats ?: $ciStats;
            if(in_array($tmpStatus,[1,9])){
                $premiumUnderwritingPrinter->statusCode = 'U1';
            }
        }


        if ($biStats == '3' or $ciStats == '3') {
            $premiumUnderwritingPrinter->statusCode = 'U2';//退回修改
            $premiumUnderwritingPrinter->messageTip = implode("\r\n", array_filter([$biTip . $ciTip]));
        }

        if ($biStats == '0' or $ciStats == '0') {
            $premiumUnderwritingPrinter->statusCode = 'U3';//人工核保
        }
        //非车信息
        if(isset($data['policyAppNoYW'])){
            $nonCarInquiryPrinter = new NonCarUnderwritingPrinter();
            /*** @var $item NonCarProductVisitor */
            $single = new NonCarInquirySinglePrinter();
            $single->inquiryNo = $data['policyAppNoYW'];
            $single->statusCode = 'success';
            $nonCarInquiryPrinter->nonCarSingleAppend($single);
            $premiumUnderwritingPrinter->noCarUnderwritingPrinter = $nonCarInquiryPrinter;
        }

        $this->printer = $premiumUnderwritingPrinter;

        return $this;
    }
}
